Make watchStart and watchMain global functions rather than classmethods,
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 15 Nov 2005 18:19:02 +0000 (19:19 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 15 Nov 2005 18:19:02 +0000 (19:19 +0100)
meaning that we no longer need to prefix all the field accesses with cls.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/xenstore/xswatch.py

index b087aeacc56e8259614d58a899ef5b09841a60be..fbf165c019b31615fe41c6ca49a0df34c8637c4d 100644 (file)
@@ -13,47 +13,45 @@ from xen.xend.XendLogging import log
 
 class xswatch:
 
-    watchThread = None
-    xs = None
-    xslock = threading.Lock()
-    
     def __init__(self, path, fn, *args, **kwargs):
         self.path = path
         self.fn = fn
         self.args = args
         self.kwargs = kwargs
-        xswatch.watchStart()
-        xswatch.xs.watch(path, self)
+        watchStart()
+        xs.watch(path, self)
+
 
+watchThread = None
+xs = None
+xslock = threading.Lock()
 
-    def watchStart(cls):
-        cls.xslock.acquire()
+def watchStart():
+    global watchThread
+    global xs
+    
+    xslock.acquire()
+    try:
+        if watchThread:
+            return
+        xs = xshandle()
+        watchThread = threading.Thread(name="Watcher", target=watchMain)
+        watchThread.setDaemon(True)
+        watchThread.start()
+    finally:
+        xslock.release()
+
+
+def watchMain():
+    while True:
         try:
-            if cls.watchThread:
-                return
-            cls.xs = xshandle()
-            cls.watchThread = threading.Thread(name="Watcher",
-                                               target=cls.watchMain)
-            cls.watchThread.setDaemon(True)
-            cls.watchThread.start()
-        finally:
-            cls.xslock.release()
-
-    watchStart = classmethod(watchStart)
-
-
-    def watchMain(cls):
-        while True:
-            try:
-                we = cls.xs.read_watch()
-                watch = we[1]
-                res = watch.fn(*watch.args, **watch.kwargs)
-                if not res:
-                    cls.xs.unwatch(watch.path, watch)
-            except:
-                log.exception("read_watch failed")
-                # Ignore this exception -- there's no point throwing it
-                # further on because that will just kill the watcher thread,
-                # which achieves nothing.
-
-    watchMain = classmethod(watchMain)
+            we = xs.read_watch()
+            watch = we[1]
+            res = watch.fn(*watch.args, **watch.kwargs)
+            if not res:
+                xs.unwatch(watch.path, watch)
+        except:
+            log.exception("read_watch failed")
+            # Ignore this exception -- there's no point throwing it
+            # further on because that will just kill the watcher thread,
+            # which achieves nothing.